package com.live.video.websocket;

import com.live.video.model.MsgEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.socket.client.WebSocketClient;

import java.util.Date;

/**
 * Description:
 * User: godd
 * Date: 2017-10-11
 * Time: 下午9:37
 */
@RestController
public class WsController {
    @Autowired
    @Lazy
    private SimpMessagingTemplate simpMessagingTemplate;

    @MessageMapping("/welcome") //发消息的地址
    @SendTo("/topic/getResponse") //订阅的地址
    public MsgEntity say(String message) {
        MsgEntity msg = new MsgEntity();
        msg.setName("server测试").setContent(message);
        return msg;
    }

    @RequestMapping("push")
    public String pushMsg(@RequestParam(name = "msg") String message) {
        MsgEntity msg = new MsgEntity();
        msg.setName("系统消息")
                .setContent(message);

        simpMessagingTemplate.convertAndSend("/topic/getResponse", msg);
        return "服务器消息推送";
    }
}
